package com.moorror.codegenvel.common.tree.actions;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAware;
import com.moorror.codegenvel.common.dialog.DialogFormTable;
import com.moorror.codegenvel.common.dialog.MRDialog;
import com.moorror.codegenvel.view.window.codegen.domain.ColumnDesc;
import com.moorror.codegenvel.view.window.codegen.domain.TableDesc;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * @Author jiyanghuang
 * @Date 2025/8/24 17:38
 */
public class SQLEditConfigAction extends AnAction implements DumbAware {

    private final Supplier<TableDesc> velocityContext;
    private final Consumer<TableDesc> consumer;
    private final String titleDialog;
    public SQLEditConfigAction(Supplier<TableDesc> velocityContext, Consumer<TableDesc> consumer, String titleDialog) {
        super("Edit SQL Exist", "Edit SQL Exist", AllIcons.Actions.Edit);
        this.velocityContext = velocityContext;
        this.consumer = consumer;
        this.titleDialog = titleDialog;
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        TableDesc tableDesc = velocityContext.get();
        if(null != tableDesc){
            DialogFormTable dialogFormTable = new DialogFormTable(e.getProject(),titleDialog, ColumnDesc.class, tableDesc.getFullColumn(), (s) -> {
                tableDesc.setFullColumn((List)s);
                consumer.accept(tableDesc);
            });
            MRDialog.of(dialogFormTable).show();
        }
    }
}
